﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NovoSerDesEva;
using Sunny.UI;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace NovoSerDes
{
    public partial class BandwidthCamera : UIPage
    {
        BandwidthCalculator parent;
        private int camTotalMpbs;

        public BandwidthCamera()
        {
            InitializeComponent();
            UIInit();
        }

        public BandwidthCamera(BandwidthCalculator _parent)
        {
            InitializeComponent();
            UIInit();
            parent = _parent;
        }

        public int TypePanelIndex
        {
            get { return typePanelIndex; }
            set { typePanelIndex = value; }
        }

        public int CamTotalMpbs
        {
            get { return camTotalMpbs; }
        }

        public BandwidthCalculator ThisParent
        {
            get { return parent; }
        }

        public List<BandwidthCameraPanel> GetTypePanels()
        {
            return TypePanels;
        }

        public Panel GetPanel2()
        {
            return panel2;
        }

        private void UIInit()
        {
            this.uiComboBox_CSIPortA.SelectedItem = "DPHY";
            CalTotalMbps();
            this.uiComboBox_TunnelPixel.SelectedItem = "Pixel";
        }

        private void CalTotalMbps()
        {
            uiTextBox_TotalMpbs.Text = (uiIntegerUpDown_Lanes.Value * uiIntegerUpDown_MbpsPerLane.Value).ToString();
            string totalMpbsText = uiTextBox_TotalMpbs.Text;
            if (int.TryParse(totalMpbsText, System.Globalization.NumberStyles.Number, null, out int totalMpbs))
            {
                camTotalMpbs = totalMpbs;
            }
        }

        private void uiIntegerUpDown_Lanes_ValueChanged(object sender, int value)
        {
            CalTotalMbps();
            parent.CalculatorAll();
        }

        private void uiIntegerUpDown_MbpsPerLane_ValueChanged(object sender, int value)
        {
            CalTotalMbps();
            parent.CalculatorAll();
        }

        private int typePanelIndex = 0;
        private List<BandwidthCameraPanel> TypePanels = new List<BandwidthCameraPanel>();
        public static int statictypePanelIndex = 0; //全局静态变量

        private void uiButton_AddType_Click(object sender, EventArgs e)
        {
            BandwidthCameraPanel TypePanel = new BandwidthCameraPanel(this, typePanelIndex); //传递父页面的引用
            //TypePanel.Text = "TypePanel" + typePanelIndex.ToString("D");
            TypePanel.Name = "TypePanel" + typePanelIndex.ToString("D");
            TypePanel.Left = 5;
            if (typePanelIndex == 0)
            {
                TypePanel.Top = typePanelIndex * (TypePanel.Height + 10);
            }
            else
            {
                BandwidthCameraPanel lastTypePanel = TypePanels[typePanelIndex - 1];
                TypePanel.Top = lastTypePanel.Top + (TypePanel.Height + 10);
            }
            panel2.Controls.Add(TypePanel);
            TypePanels.Add(TypePanel);
            typePanelIndex++;
            statictypePanelIndex++;

            parent.CalculatorAll();
        }

        private void uiButton_RemoveType_Click(object sender, EventArgs e)
        {
            if (TypePanels.Count > 0)
            {
                BandwidthCameraPanel lastTypePanel = TypePanels[TypePanels.Count - 1];
                panel2.Controls.Remove(lastTypePanel);
                TypePanels.Remove(lastTypePanel);
                typePanelIndex--;
                parent.CalculatorAll();
            }
        }
    }
}
